home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / test / results.c
Encoding:
C/C++ Source or Header  |  2001-11-01  |  7.3 KB  |  415 lines

  1. /* err/test_results.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman, Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <math.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #ifdef HAVE_VPRINTF
  27. #ifdef STDC_HEADERS
  28. #include <stdarg.h>
  29. #else
  30. #include <varargs.h>
  31. #endif
  32. #endif
  33.  
  34. #include <gsl/gsl_test.h>
  35.  
  36. static unsigned int tests = 0;
  37. static unsigned int passed = 0;
  38. static unsigned int failed = 0;
  39.  
  40. static unsigned int verbose = 1;
  41.  
  42. void
  43. gsl_test (int status, const char *test_description,...)
  44. {
  45.  
  46.   tests++;
  47.  
  48.   if (status == 0)
  49.     {
  50.       passed++;
  51.       if (verbose)
  52.     printf ("PASS: ");
  53.     }
  54.   else
  55.     {
  56.       failed++;
  57.       if (verbose)
  58.     printf ("FAIL: ");
  59.     }
  60.  
  61.   if (verbose)
  62.     {
  63.  
  64. #ifdef HAVE_VPRINTF
  65.       va_list ap;
  66.  
  67. #ifdef STDC_HEADERS
  68.       va_start (ap, test_description);
  69. #else
  70.       va_start (ap);
  71. #endif
  72.       vprintf (test_description, ap);
  73.       va_end (ap);
  74. #endif
  75.  
  76.       printf("\n");
  77.       fflush (stdout);
  78.     }
  79. }
  80.  
  81.  
  82. void
  83. gsl_test_rel (double result, double expected, double relative_error,
  84.           const char *test_description,...)
  85. {
  86.   int status ;
  87.  
  88.   if (expected != 0 ) 
  89.     {
  90.       status = (fabs(result-expected)/fabs(expected) > relative_error) ;
  91.     }
  92.   else
  93.     {
  94.       status = (fabs(result) > relative_error) ;
  95.     }
  96.  
  97.   tests++;
  98.  
  99.   if (status == 0)
  100.     {
  101.       passed++;
  102.       if (verbose)
  103.     printf ("PASS: ");
  104.     }
  105.   else
  106.     {
  107.       failed++;
  108.       if (verbose)
  109.     printf ("FAIL: ");
  110.       
  111.     }
  112.  
  113.   if (verbose)
  114.     {
  115.  
  116. #ifdef HAVE_VPRINTF
  117.       va_list ap;
  118.  
  119. #ifdef STDC_HEADERS
  120.       va_start (ap, test_description);
  121. #else
  122.       va_start (ap);
  123. #endif
  124.       vprintf (test_description, ap);
  125.       va_end (ap);
  126. #endif
  127.       if (status == 0)
  128.     {
  129.       if (strlen(test_description) < 45)
  130.         {
  131.           printf(" (%g observed vs %g expected)", result, expected) ;
  132.         }
  133.       else
  134.         {
  135.           printf(" (%g obs vs %g exp)", result, expected) ;
  136.         }
  137.     }
  138.       else 
  139.     {
  140.       printf(" (%.18g observed vs %.18g expected)", result, expected) ;
  141.     }
  142.  
  143.       printf ("\n") ;
  144.       fflush (stdout);
  145.     }
  146. }
  147.  
  148. void
  149. gsl_test_abs (double result, double expected, double absolute_error,
  150.           const char *test_description,...)
  151. {
  152.   int status ;
  153.  
  154.   status = fabs(result-expected) > absolute_error ;
  155.  
  156.   tests++;
  157.  
  158.   if (status == 0)
  159.     {
  160.       passed++;
  161.       if (verbose)
  162.     printf ("PASS: ");
  163.     }
  164.   else
  165.     {
  166.       failed++;
  167.       if (verbose)
  168.     printf ("FAIL: ");
  169.       
  170.     }
  171.  
  172.   if (verbose)
  173.     {
  174.  
  175. #ifdef HAVE_VPRINTF
  176.       va_list ap;
  177.  
  178. #ifdef STDC_HEADERS
  179.       va_start (ap, test_description);
  180. #else
  181.       va_start (ap);
  182. #endif
  183.       vprintf (test_description, ap);
  184.       va_end (ap);
  185. #endif
  186.       if (status == 0)
  187.     {
  188.       if (strlen(test_description) < 45)
  189.         {
  190.           printf(" (%g observed vs %g expected)", result, expected) ;
  191.         }
  192.       else
  193.         {
  194.           printf(" (%g obs vs %g exp)", result, expected) ;
  195.         }
  196.     }
  197.       else 
  198.     {
  199.       printf(" (%.18g observed vs %.18g expected)", result, expected) ;
  200.     }
  201.  
  202.       printf ("\n") ;
  203.       fflush (stdout);
  204.     }
  205. }
  206.  
  207.  
  208. void
  209. gsl_test_factor (double result, double expected, double factor,
  210.                  const char *test_description,...)
  211. {
  212.   int status;
  213.   
  214.   if (result == expected) 
  215.     {
  216.       status = 0;
  217.     }
  218.   else if (expected == 0.0) 
  219.     {
  220.       status = (result > expected || result < expected);
  221.     }
  222.   else
  223.     {
  224.       double u = result / expected; 
  225.       status = (u > factor || u < 1.0 / factor) ;
  226.     }
  227.  
  228.   tests++;
  229.  
  230.   if (status == 0)
  231.     {
  232.       passed++;
  233.       if (verbose)
  234.     printf ("PASS: ");
  235.     }
  236.   else
  237.     {
  238.       failed++;
  239.       if (verbose)
  240.     printf ("FAIL: ");
  241.       
  242.     }
  243.  
  244.   if (verbose)
  245.     {
  246.  
  247. #ifdef HAVE_VPRINTF
  248.       va_list ap;
  249.  
  250. #ifdef STDC_HEADERS
  251.       va_start (ap, test_description);
  252. #else
  253.       va_start (ap);
  254. #endif
  255.       vprintf (test_description, ap);
  256.       va_end (ap);
  257. #endif
  258.       if (status == 0)
  259.     {
  260.       if (strlen(test_description) < 45)
  261.         {
  262.           printf(" (%g observed vs %g expected)", result, expected) ;
  263.         }
  264.       else
  265.         {
  266.           printf(" (%g obs vs %g exp)", result, expected) ;
  267.         }
  268.     }
  269.       else 
  270.     {
  271.       printf(" (%.18g observed vs %.18g expected)", result, expected) ;
  272.     }
  273.  
  274.       printf ("\n") ;
  275.       fflush (stdout);
  276.     }
  277. }
  278.  
  279. void
  280. gsl_test_int (int result, int expected, const char *test_description,...)
  281. {
  282.   int status = (result != expected) ;
  283.  
  284.   tests++;
  285.  
  286.   if (status == 0)
  287.     {
  288.       passed++;
  289.       if (verbose)
  290.     printf ("PASS: ");
  291.     }
  292.   else
  293.     {
  294.       failed++;
  295.       if (verbose)
  296.     printf ("FAIL: ");
  297.     }
  298.  
  299.   if (verbose)
  300.     {
  301.  
  302. #ifdef HAVE_VPRINTF
  303.       va_list ap;
  304.  
  305. #ifdef STDC_HEADERS
  306.       va_start (ap, test_description);
  307. #else
  308.       va_start (ap);
  309. #endif
  310.       vprintf (test_description, ap);
  311.       va_end (ap);
  312. #endif
  313.       if (status == 0)
  314.     {
  315.       printf(" (%d observed vs %d expected)", result, expected) ;
  316.     }
  317.       else 
  318.     {
  319.       printf(" (%d observed vs %d expected)", result, expected) ;
  320.     }
  321.  
  322.       printf ("\n");
  323.       fflush (stdout);
  324.     }
  325. }
  326.  
  327. void
  328. gsl_test_str (const char * result, const char * expected, 
  329.           const char *test_description,...)
  330. {
  331.   int status = strcmp(result,expected) ;
  332.  
  333.   tests++;
  334.  
  335.   if (status == 0)
  336.     {
  337.       passed++;
  338.       if (verbose)
  339.     printf ("PASS: ");
  340.     }
  341.   else
  342.     {
  343.       failed++;
  344.       if (verbose)
  345.     printf ("FAIL: ");
  346.     }
  347.  
  348.   if (verbose)
  349.     {
  350.  
  351. #ifdef HAVE_VPRINTF
  352.       va_list ap;
  353.  
  354. #ifdef STDC_HEADERS
  355.       va_start (ap, test_description);
  356. #else
  357.       va_start (ap);
  358. #endif
  359.       vprintf (test_description, ap);
  360.       va_end (ap);
  361. #endif
  362.       if (status)
  363.     {
  364.       printf(" (%s observed vs %s expected)", result, expected) ;
  365.     }
  366.  
  367.       printf ("\n");
  368.       fflush (stdout);
  369.     }
  370. }
  371.  
  372.  
  373.  
  374.  
  375. void
  376. gsl_test_verbose (int v)
  377. {
  378.   verbose = v;
  379. }
  380.  
  381. int
  382. gsl_test_summary (void)
  383. {
  384.  
  385.   if (verbose && 0)        /* FIXME: turned it off, this annoys me */
  386.     printf ("%d tests, passed %d, failed %d.\n", tests, passed, failed);
  387.  
  388.   if (failed != 0)
  389.     {
  390.  
  391.       if (verbose && 0)        /* FIXME: turned it off, this annoys me */
  392.     {
  393.       printf ("%d TEST%s FAILED.\n", failed, failed == 1 ? "" : "S");
  394.     }
  395.       return EXIT_FAILURE;
  396.     }
  397.  
  398.   if (tests != passed + failed)
  399.     {
  400.       if (verbose)
  401.     printf ("TEST RESULTS DO NOT ADD UP %d != %d + %d\n",
  402.         tests, passed, failed);
  403.       return EXIT_FAILURE;
  404.     }
  405.  
  406.   if (passed == tests)
  407.     {
  408.       if (verbose && 0)        /* FIXME: turned it off, this annoys me */
  409.     printf ("All tests passed successfully\n");
  410.       return EXIT_SUCCESS;
  411.     }
  412.  
  413.   return EXIT_FAILURE;
  414. }
  415.